package com.qf.smartdevice.webscoket;

import com.qf.smartdevice.event.PowerCommandEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.config.annotation.EnableWebSocket;

import java.io.IOException;

@Component
@EnableWebSocket
public class PowerCommandWebsocket extends MyBaseWebsocketHandler {
    @EventListener
    public void onEvent(PowerCommandEvent event) {
//        发送的指令
//        谁发的指令
        String uuid = event.getUuid();
//        发送什么指令
        String command = event.getCommand();

        WebSocketSession session = allClients.get(uuid);

        if (session != null && session.isOpen()) {
            try {
                session.sendMessage(new TextMessage(command));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
